import axios from 'axios'
import config from '../config/index'
import { ElMessage } from 'element-plus'
import { useCookies } from "vue3-cookies";
import { useRouter } from "vue-router";
const router = useRouter();
const { cookies } = useCookies();
cookies.set('k2', 'v2', '1h')
// 获取
console.log(cookies.get('k2')) // v2
var xhr = new XMLHttpRequest()
xhr.withCredentials = true
class Http {

  static instance;

  // 单例模式
  static getInstance() {
    if (this.instance instanceof this) {
      return this.instance;
    } else {
      this.instance = new this;
      return this.instance;
    }
  }

  constructor() {
    this.SetBaseUrl();
    this.interceptors();
  }
  async get(url, params = {}) {
    return await axios.get(url, {
      params: params,
      headers: Object.assign(
        {
          token: localStorage.getItem("token"),
          Authorization: localStorage.getItem("token")
        }
      )
    })
  }

  async post(url, params = {}, header = {}) {
    return await axios.post(url, params, {
      headers: Object.assign(
        header,
        {
          token: localStorage.getItem("token"),
          Authorization: localStorage.getItem("token")
        }
      )
    })
  }

  async put(url, params = {}, header = {}) {
    return await axios.put(url, params, {
      headers: Object.assign(
        header,
        {
          token: localStorage.getItem("token"),
          Authorization: localStorage.getItem("token")
        }
      )
    })
  }

  async delete(url, params = {}, header = {}) {
    return await axios.delete(url, params,{
      headers: Object.assign(
        {
          header,
          token: localStorage.getItem("token"),
          Authorization: localStorage.getItem("token")
        }
      )
    })
  }

  async postFile(url, params = {}, header = {}) {
    return await axios.postFile(url, params, {
      headers: Object.assign(
        header,
        {
          Authorization: localStorage.getItem("token")
        }
      ),
      responseType: 'arraybuffer'
    })
  }

  interceptors() {
    /**
     * 请求拦截器
     */
    axios.interceptors.request.use(function (config) {
      return config;
    }, function (error) {
      // 对请求错误做些什么
      console.log(11);
      return Promise.reject(error);
    });

    /**
     * 响应拦截器 
     * 在这里处理返回数据的状态码
     */
    axios.interceptors.response.use(async (response) => {
      console.log('response', response);
      // 对响应数据做点什么
      switch (response.data.code) {
        case 401:
          ElMessage({
            type: 'error',
            message: 'token已过期，重新登录',
          })
          router.push({ path: '/login' });
          break;
        case 10004:
          ElMessage({
            type: 'error',
            message: response.data.msg,
          })
          break;
        case 500:
          ElMessage({
            type: 'error',
            message: response.data.msg,
          })
          break;

        default:
          return response.data;
      }
    }, function (error) {
      if (302 === error.response.status) {
        window.location.href = "http://47.101.219.237:8081/renren-admin"//此处加跳转链接
      } else {
        return Promise.reject(error);
      }
      // return Promise.reject(error);
    });
  }
  /**
   * 设置请求基地址
   */
  SetBaseUrl() {
    var xhr = new XMLHttpRequest()
    xhr.withCredentials = true
    axios.defaults.baseURL = config.check();
    axios.defaults.withCredentials = true; //意思是携带cookie信息
  }

}

export default Http
